using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;

namespace WindowsAPI.Windows
{
    public partial class Window
    {
        [Serializable]
        public class RemotingHost : MarshalByRefObject, IDisposable
        {
            private static readonly List<WeakReference> sf_HostsPool = new List<WeakReference>();

            private static Dictionary<string, WellKnownServiceTypeEntry> sf_RegistredServices =
                        new Dictionary<string, WellKnownServiceTypeEntry>();

            public static IDictionary<string, WellKnownServiceTypeEntry> ServicessPool
            {
                get { return sf_RegistredServices; }
            }

            public static IChannel[] RegistredChannels { get { return ChannelServices.RegisteredChannels; } }

            public static void RegisterChannel(IChannel Channel, bool Security) { ChannelServices.RegisterChannel(Channel, Security); }

            public static void UnRegisterChannel(IChannel Channel) { ChannelServices.UnregisterChannel(Channel); }

            public static WellKnownServiceTypeEntry RegisterHost(IChannel Channel, bool Security, string Uri, WellKnownObjectMode Mode)
            {
                RegisterChannel(Channel, Security);
                return RegisterHost(Uri, Mode);
            }

            public static WellKnownServiceTypeEntry RegisterHost(string Uri, WellKnownObjectMode Mode)
            {
                if(sf_RegistredServices.ContainsKey(Uri))
                    return sf_RegistredServices[Uri];

                var entry = new WellKnownServiceTypeEntry(typeof(RemotingHost), Uri, Mode);
                RemotingConfiguration.RegisterWellKnownServiceType(entry);
                sf_RegistredServices.Add(Uri, entry);

                return entry;
            }

            //public static void UnRegister(string Uri) { UnRegister(sf_RegistredServices[Uri]); }
            //private static void UnRegister(WellKnownServiceTypeEntry Service)
            //{

            //}

            public static RemotingHost Create(IChannel Channel, bool Security, string URL)
            {
                ChannelServices.RegisterChannel(Channel, Security);
                return (RemotingHost)Activator.GetObject(typeof(RemotingHost), URL);
            }

            public WindowsPool HostPool { get { return Pool; } }

            static RemotingHost() { RemotingConfiguration.ApplicationName = typeof(Window).FullName; }

            public RemotingHost() { sf_HostsPool.Add(new WeakReference(this)); }

            public void Dispose() { sf_HostsPool.RemoveAll(w => w.Target.Equals(this)); }

            public Window GetHostWindow(IntPtr Handle) { return new Window(Handle); }

            public Window GetHostWindow(string Title, string ClassName = null) { return GetWindow(ClassName, Title); }

            public Window GetHostWindow(int X, int Y) { return GetWindow(Y, Y); }

            public Window GetHostWindow(Point Location) { return GetWindow(Location); }
        }
    }
}